home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / network / lattice / portlib.lzh / PORTLIB / TIMEOFDA.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-06  |  1.2 KB  |  63 lines

  1. #include <sys/types.h>
  2. #include <sys/time.h>
  3.  
  4. /*
  5.  * In the MiNT library, gettimeofday has a granularity of two seconds --
  6.  * which is pretty useless for ping.
  7.  * We use clock() instead, which measures real time instead of process
  8.  * time in the MiNT library. (clock() has 5 ms granularity).
  9.  */
  10.  
  11. static int first = 1;
  12. static struct timeval the_time;
  13. static clock_t oticks;
  14.  
  15. static void
  16. tvadd (tv1, tv2)
  17.     struct timeval *tv1, *tv2;
  18. {
  19.     tv1->tv_usec += tv2->tv_usec;
  20.     if ((unsigned long)tv1->tv_usec > 1000000L) {
  21.         tv1->tv_usec -= 1000000L;
  22.         tv1->tv_sec++;
  23.     }
  24.     tv1->tv_sec += tv2->tv_sec;
  25. }
  26.  
  27. int
  28. __5ms_gettimeofday (tv, tz)
  29.     struct timeval *tv;
  30.     struct timezone *tz;
  31. {
  32.     clock_t diff, ticks = clock ();
  33.  
  34.     if (first) {
  35.         first = 0;
  36.         oticks = ticks;
  37.         gettimeofday (&the_time, 0);
  38.     }
  39.     diff = ticks - oticks;
  40.     oticks = ticks;
  41.  
  42.     tv->tv_sec  = diff / CLK_TCK;
  43.     tv->tv_usec = ((diff % CLK_TCK)*1000000L) / CLK_TCK;
  44.     tvadd (&the_time, tv);
  45.     *tv = the_time;
  46.     return 0;
  47. }
  48.  
  49. int
  50. __5ms_settimeofday (tv, tz)
  51.     struct timeval *tv;
  52.     struct timezone *tz;
  53. {
  54.     int r;
  55.  
  56.     r = settimeofday (tv, tz);
  57.     if (r == 0) {
  58.         the_time = *tv;
  59.         oticks = clock ();
  60.     }
  61.     return r;
  62. }
  63.